package com.breakfast.fun.ui.user.cart;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.home.cart.SendTime;
import com.breakfast.fun.data.entity.home.product.ProductState;
import com.breakfast.fun.data.entity.user.coupon.Coupon;
import com.breakfast.fun.data.entity.user.recharge.RechargeMoney;
import com.breakfast.fun.data.entity.user.submit.OrderInfo;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/8.
 */
public class UserCartPresenter extends UserCartContract.Presenter {
    @Override
    public void onStart() {
        getSendDate();
        getCoupon();
    }

    @Override
    public void getSendTime() {

        Map<String, String> map = Api.getInstance().getBaseParams();

        showLoadingDialog(true);

        rxManager.add(model.getSendTimeModel(map).
                subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<SendTime>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(SendTime sendTime) {
                        hideLoadingDialog();
                        if (sendTime.isSuccess()) {
                            view.setSendTime(sendTime.getData());
                        } else {
                            T.show(sendTime.getMsg());
                        }
                    }

                }));

    }

    @Override
    public void getSendDate() {

        Map<String, String> map = Api.getInstance().getBaseParams();
        map.put("action", "weekday");

        showLoadingDialog(true);

        rxManager.add(model.getProductStateModel(map).
                subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ProductState>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(ProductState productState) {
                        hideLoadingDialog();
                        if (productState.isSuccess()) {
                            view.setSendDate(productState.getData());
                        } else {
                            T.show(productState.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void submitOrder(String order) {
        Map<String, String> map = Api.getInstance().getBaseParams();
        map.put("action", "packages");
        map.put("orderInfo", order);

        showLoadingDialog(true);

        rxManager.add(model.getSubmitOrderModel(map).
                subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<OrderInfo>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(OrderInfo orderInfo) {
                        hideLoadingDialog();
                        if (orderInfo.isSuccess()) {
                            getPayBean(orderInfo.getData().getOrderNo());
                        } else {
                            T.show(orderInfo.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void getPayBean(String orderNo) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("orderNo", orderNo);
        map.put("orderType", "1");

        showLoadingDialog(false);
        rxManager.add(model.getPayBeanModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<RechargeMoney>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(RechargeMoney rechargeMoney) {
                        hideLoadingDialog();
                        if (rechargeMoney.isSuccess()) {
                            view.showOrderInfo(rechargeMoney.getData());
                        } else {
                            T.show(rechargeMoney.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void getCoupon() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        if (Constant.getUserData() != null) {
            map.put("userId", Constant.getUserData().getId() + "");
            map.put("begin", "0");
            map.put("end", "100");

        } else {
//            JumpUtil.toLogin(App.getAppContext());
            return;
        }

        showLoadingDialog(true);

        rxManager.add(model.getProperCouponModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Coupon>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.show("获取数据失败");
                        hideLoadingDialog();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Coupon coupon) {
                        hideLoadingDialog();
                        if (coupon.isSuccess()) {
                            view.setCoupon(coupon);
                        } else {
//                            T.show(coupon.getMsg());
                        }
                    }

                }));
    }
}
